package com.yangxk.kadmin.generator.internal.field;

import com.yangxk.kadmin.generator.field.BetweenFiled;
import com.yangxk.kadmin.generator.field.Field;
import com.yangxk.kadmin.generator.rules.Match;
import com.yangxk.kadmin.generator.util.Tools;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 字段范围查询解析
 * @author yangxk
 * @date 2018/10/5 13:48
 */
public class BetweenFieldInternal {
    public static final String BEGIN_PREFIX = "begin";
    public static final String END_PREFIX = "end";

    public static List<Field> createField(Field field) {
        String fieldName = Tools.first2Upper(field.getPropertyName());
        String begin = BEGIN_PREFIX + fieldName;
        String end = END_PREFIX + fieldName;

        BetweenFiled beginField = new BetweenFiled();
        BetweenFiled endField = new BetweenFiled();

        BeanUtils.copyProperties(field, beginField);
        BeanUtils.copyProperties(field, endField);

        beginField.setPropertyName(begin);
        beginField.setMatch(Match.egt);
        beginField.setOriginalPropertyName(fieldName);

        endField.setPropertyName(end);
        endField.setMatch(Match.elt);
        endField.setOriginalPropertyName(fieldName);

        beginField.setPairName(endField.getPropertyName());
        endField.setPairName(beginField.getPropertyName());

        List<Field> result = new ArrayList<>(2);
        result.add(beginField);
        result.add(endField);

        return result;
    }
}
